package com.moc.common.utils.sign;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA加密解密
 *
 * @author ruoyi
 **/
public class RsaUtils
{
    // Rsa 私钥
    public static String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCiz6Wzve36uFJ5" +
            "IxEyqj/SgHT/86EhtMQuCQq+CFH+NioiR3sxNv/BXtutS1ipo0Dq3mgQrwQLkFeMBeEQF2ZdgZ7jauJ7wvUe3LcF6WX" +
            "G0ul9nZPZIJfI4oF9sZFr9+Ar1TW6n44gCqfkjZMmjbGZNW4uFLBrRl7JKGqRRAB8+leXkFZYa2Ohy2aKUrL61S8oE9" +
            "zHPiqElCAIk8c63FebeQFOOawILglvaHQFMQq/0GtMk1NkoqEVO5d+KCiIgWfplPWMRDkKwPO57nEkV4QPrm8EmkD7h" +
            "4ODPzqBq0b+QB5frtZggQCVdeAaJiaFJXM/UhhfoBkkrW6IN6mLq/XRAgMBAAECggEAFhhUBoaaC1KEbsghh1yveKPS" +
            "PQIVNfvQO+ciPBPo/LThFvHYeB9cqiNzcJsvFyZ+ouE4AjueMuuN1gTePE7Qp5P1uitBPUaB+YQ+mmDIXSk2YsPF0su" +
            "nja2foS097QcgtxIiTodkxFht3IiSFcNY/M/RehN4tbZBGs+PVtOBtXQUjcEw0w459miFvMI3OWDT8RasQctp1ns92p" +
            "Z/c6kUtTaqDnvKMQ8hJo2cAPI30fVGbUyVbvC7qWe71X4el7e5vEOrN5/zpP7+mOp/GuNPrkPb/ELJ1+SM4/Ldcjhj9" +
            "WbromNEI3yZRP8c4Y4x2WM1bfcYjdBiRnWfCKyXNZAJgQKBgQDQ2CJh3Lj/2ufUqXmHZ2ak5+AZNgUNhjo9yD93P2m5" +
            "ePw1XpG9DyGshOnrXqVtst59xvTIvIjxpFFeaUxQcY5H/prRWwClvXBy/oF5fJ2jcPJBh0a2FVwixazPJ5VmOpXHMFp" +
            "jSNvkAJeOXhvxSgh2/WQZRd71U2H00TUyeCLluQKBgQDHkqZamYs3B3cNUotXG7DvKv/4+dTmXZffqmo+iO6MdAUxkP" +
            "Z+LMNN9S4KC4LmGQ9kF94hyJAyv0fn/fLPkw4GL45y2WIbXOWOJdXai0arcMCaZxVPTDlm1AezVjuxvPZbSsNsZJc3X" +
            "J1Ua8K6G3qCERkKpTZJNjZrqPlfeC8c2QKBgDoF0oed60gqaX6MF1enQr1OLtTSTVUUldnUaM2YsKb3Fhs9yv/b1F86" +
            "DkN2aD+yEl6fiCK9Ps7j+DdiyfEPm5glBJ5lyihCGZvZWd+G38Uo/CV4kAzGonKGuRxUww1D+Zj9th8ffvg5seG8A8d" +
            "l6jQk8kqTPv6py+1Yso7zEb8BAoGBAKehvqmLHyxx0MYX94KLd+rsq2VuNC3rn433jpZIXlcLfPFNWj5RSxtIPguh76" +
            "lmk0733VVzKev2T2fGHxXL55O6jmRZw9ZICWmEzkRriEioxJ7sxzF8TRraLj2sySdaGXRFMDDzrTCLUnt+qGm6ewSVZ" +
            "/tRGnAUYVG89PoD8U7BAoGAQ8G8hoOEvScTc4YIy82Of8EN29HBimbfkwr4h9m0Z3prbgDc3w9/YaTtd8baD+Li5Fn/" +
            "zq14MB0oyV623F/iHowMwp4dS3/0bCTMLPvK/0KnFwXNIKz5xJRPKhR2lDxsqFj02NtkytifMREsi0PE3GZp9BDJzAxWMFuRg03DVD0=";

    /**
     * 私钥解密
     *
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text) throws Exception
    {
        return decryptByPrivateKey(privateKey, text);
    }

    /**
     * 公钥解密
     *
     * @param publicKeyString 公钥
     * @param text 待解密的信息
     * @return 解密后的文本
     */
    public static String decryptByPublicKey(String publicKeyString, String text) throws Exception
    {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 私钥加密
     *
     * @param privateKeyString 私钥
     * @param text 待加密的信息
     * @return 加密后的文本
     */
    public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception
    {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception
    {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 公钥加密
     *
     * @param publicKeyString 公钥
     * @param text 待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String publicKeyString, String text) throws Exception
    {
        X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 构建RSA密钥对
     *
     * @return 生成后的公私钥信息
     */
    public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException
    {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
        String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
        return new RsaKeyPair(publicKeyString, privateKeyString);
    }

    /**
     * RSA密钥对对象
     */
    public static class RsaKeyPair
    {
        private final String publicKey;
        private final String privateKey;

        public RsaKeyPair(String publicKey, String privateKey)
        {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }

        public String getPublicKey()
        {
            return publicKey;
        }

        public String getPrivateKey()
        {
            return privateKey;
        }
    }
}
